package com.dudu.dao;

import java.sql.Connection;
import java.util.Calendar;
import java.util.List;

import net.sf.json.JSONObject;

import com.dudu.model.SmsLog;

import core.CoreDao;
import core.CoreDaoInvoke;
import core.db.DBConnectionUtil;

public class SmsLogDao {

	public static void main(String[] args) {

		CoreDao coreDao = new CoreDaoInvoke();
		SmsLogDao dao = new SmsLogDao();
		
		SmsLog bean = new SmsLog();
		bean.setIsv_name("用友医疗");
		bean.setPhoneNbr("17001090122");
		bean.setSms_content("尊敬的能力平台用户，截止"+Calendar.getInstance().getTime().toLocaleString()+"您的账户余额为：99.00元，请充值！（用友通信能力平台）");

		dao.insert(coreDao, bean);
	}
	
	public int insert(CoreDao dao, SmsLog bean) {
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_sms_log(phoneNbr, isv_name, sms_content, send_time, send_result)");
			sql.append("VALUES(?, ?, ?, SYSDATE(), ?)");

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { 
						bean.getPhoneNbr(), bean.getIsv_name(), bean.getSms_content(), bean.getSend_result()
					});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public List<SmsLog> queryAllIsvByType(CoreDao dao) {
		Connection con = null;
		List<SmsLog> result = null;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"select * FROM t_sms_log");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] {  });
			if (list != null && list.size() > 0) {
				result = dao.covertToBean(list, SmsLog.class);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
}
